#!/bin/bash
# -*- mode: sh -*-

MAINLANG=russian
ALTLANG=english

source scripts/include/header
source scripts/include/function


# {{{ Make preamble

for i in ${MAINLANG}
do
    touch ${out_dir}/${i}.tex
    cat support/${PROJECT_NAME}/preamble/pre/*.tex >> ${out_dir}/${i}.tex
    # cat support/${PROJECT_NAME}/preamble/${i}.tex >> ${out_dir}/${i}.tex
    cat support/${PROJECT_NAME}/preamble/post/*.tex >> ${out_dir}/${i}.tex
    
    cp ${i}.bbl ${out_dir}/

    cat ${out_dir}/${i}.tex >> ${out_dir}/text/${out_name}.tex
done

# }}}


# {{{ title

shift
for file in "$@"
do
    # Из head берём только основной язык
    # if [[ $file == +(title/*head*) ]]
    # then

    # 	clear_all_lang

    # 	cat ./${out_dir}/${MAINLANG}_clean.tex >> ./${out_dir}/text/${out_name}.tex
    # 	# cat ./${out_dir}/${ALTLANG}_clean.tex >> ./${out_dir}/text/${out_name}.tex
	
    # 	rm ./${out_dir}/*.tex
	
    # 	shift
    # 	continue

    # fi

    if [[ $file == +(title/*) ]]
    then
	case "$file" in
	    title/*head* )
		shift
		continue
		;;
	    title/*title* )
		shift
		continue
		;;
	    title/*author* )
		shift
		continue
		;;
	    * )
		clear_all_lang
		
		# clear_maintitle
		# clear_alttitle
		
		# Только основной язык
		cat ./${out_dir}/${MAINLANG}_clean.tex >> ./${out_dir}/text/${out_name}.tex
		# cat ./${out_dir}/${ALTLANG}_clean.tex >> ./${out_dir}/text/${out_name}.tex
		
		rm ./${out_dir}/*.tex
		
		shift
		;;
	esac
    fi
done

# }}}

# {{{ body

for file in "$@"
do

    clear_all_lang
    
    # Нужен только один язык
    cat ./${out_dir}/${MAINLANG}_clean.tex >> ./${out_dir}/text/${out_name}.tex
    # cat ./${out_dir}/${ALTLANG}_clean.tex >> ./${out_dir}/text/${out_name}.tex
    
    rm ./${out_dir}/*.tex
    
done

# {{{ Библиография

sed -i -e '/^[[:space:]]*\\bibliography.*/D' ${out_dir}/text/${out_name}.tex

sed -i -e '/^\\newblock URL.*/D' \
    -e 's:\\providecommand\*{\\BibEmph}\[1\]{\#1}:\\providecommand*{\\BibEmph}[1]{\\emph{#1}}:g' \
    ${out_dir}/*.bbl

cat ${out_dir}/*.bbl >> ${out_dir}/text/${out_name}.tex

# }}}

clear_comment "${out_dir}/text/${out_name}.tex"

# }}}

# {{{ Make postamble

cat support/${PROJECT_NAME}/preamble/end/* >> ${out_dir}/text/${out_name}.tex

# }}}

# {{{ Ecms hacks

# sed -i -e 's:\\begin{acknowledgments}:\\paragraph{Acknowledgments.}:g' \
#     ${out_dir}/text/${out_name}.tex

# sed -i -e '/\\end{acknowledgments}/D' \
#     ${out_dir}/text/${out_name}.tex

# sed -i -e 's:\\section{:\\Section{:g' \
#     -e 's:\\subsection{:\\Subsection{:g' \
#     ${out_dir}/text/${out_name}.tex

# }}}

#{{{ Для журнала Программирование

sed -i -e 's:\\section:\\He:' ${out_dir}/text/${out_name}.tex
sed -i -e 's:\\subsection:\\he:' ${out_dir}/text/${out_name}.tex
sed -i -e '/^\\maketitle.*/D' ${out_dir}/text/${out_name}.tex
sed -i -e 's:\\begin{abstract}:\\rusabstr{:' ${out_dir}/text/${out_name}.tex
sed -i -e 's:\\end{abstract}:}\\maketitle:' ${out_dir}/text/${out_name}.tex

#}}}

clear_garbage

#{{{ UTF8 -> CP1251

echo -e '\n\n%%% Local Variables:\n%%% mode: latex\n%%% coding: cp1251-unix\n%%% End:\n' >> ${out_dir}/text/${out_name}.tex
find ${out_dir} -name "*.tex" -exec recode -f UTF-8..WINDOWS1251 "{}" \;

# find ${out_dir} -name "*.bib" -exec echo -e '\n\n%%% Local Variables:\n%%% mode: bibtex\n%%% coding: cp1251-unix\n%%% End:\n' >> "{}" \;
# find ${out_dir} -name "*.bib" -exec recode -f UTF-8..WINDOWS1251 "{}" \;

#}}}


# {{{ Copy support files

if [[ -d support/${PROJECT_NAME}/files ]]
then
    cp support/"${PROJECT_NAME}"/files/* "${out_dir}"/text/
fi

# }}}
